package sources;
import java.awt.event.*;
import java.awt.*;
import java.io.File;

import javax.swing.*;
/**
 * ICSFileCreatorStartFrame
 * 
 * Combines the JFrame components together and builds the actual GUI.
 *
 * @author Alana Meditz
 * @author Ty Yago
 * @author Katherine Kishaba
 * @author Jimmy CUmming
 * 
 */
public class ICSFileCreatorStartFrame extends JFrame {
	private static final int WIDTH = 500;
	private static final int HEIGHT = 400;
	
	private JLabel promptL;
	private JButton addEventB, addToDoB, addJournalB, addFreeBusyB, addTimeZoneB,
		addOtherComponentB, restartB, saveB, viewB, exitB;
	private JFileChooser fc = new JFileChooser();
	private AddEventButtonHandler addEventBH;
	private RestartButtonHandler restartBH;
	private ExitButtonHandler exitBH;
	private SaveButtonHandler saveBH;
	private AddToDoButtonHandler addToDoBH;
	private AddJournalButtonHandler addJournalBH;
	private AddFreeBusyButtonHandler addFreeBusyBH;
	private AddTimeZoneButtonHandler addTimeZoneBH;
	private ViewButtonHandler viewBH;

	private ICalendar iCal = new ICalendar();
	/**
	 * Constructor takes no args.
	 */
	public ICSFileCreatorStartFrame() {
		setTitle("ICSFileCreatorHome");
		setSize(WIDTH, HEIGHT);
		setVisible(true);
		
		promptL = new JLabel("What would you like to do?", SwingConstants.RIGHT);
		
		instantiateButtons();
		
		setUpPane();
	}
	
	/**
	 * Private function used by constructor to set up event handlers.
	 */
	private void instantiateButtons() {
		addEventB = new JButton("Add Event");
		addEventBH = new AddEventButtonHandler();
		addEventB.addActionListener(addEventBH);
		
		addToDoB = new JButton("Add ToDo");
		addToDoBH = new AddToDoButtonHandler();
		addToDoB.addActionListener(addToDoBH);
		
		addJournalB = new JButton("Add Journal");
		addJournalBH = new AddJournalButtonHandler();
		addJournalB.addActionListener(addJournalBH);
		
		addFreeBusyB = new JButton("Add FreeBusy");
		addFreeBusyBH = new AddFreeBusyButtonHandler();
		addFreeBusyB.addActionListener(addFreeBusyBH);
		
		addTimeZoneB = new JButton("Select TimeZone");
		addTimeZoneBH = new AddTimeZoneButtonHandler();
		addTimeZoneB.addActionListener(addTimeZoneBH);
		
		viewB = new JButton("View");
		viewBH = new ViewButtonHandler();
		viewB.addActionListener(viewBH);
		
		saveB = new JButton("Save");
		saveBH = new SaveButtonHandler();
		saveB.addActionListener(saveBH);
		
		restartB = new JButton("Restart");
		restartBH = new RestartButtonHandler();
		restartB.addActionListener(restartBH);
		
		exitB = new JButton("Exit");
		exitBH = new ExitButtonHandler();
		exitB.addActionListener(exitBH);
	}
	
	/**
	 * Private function used by constructor to place all elements on the GUI.
	 */
	private void setUpPane() {
		Container pane = getContentPane();
		pane.setLayout(new GridLayout(3, 3));
		
		// Add items to pane
		pane.add(addEventB);
		pane.add(addToDoB);
		pane.add(addJournalB);
		pane.add(addFreeBusyB);
		pane.add(addTimeZoneB);
		pane.add(viewB);
		pane.add(saveB);
		pane.add(restartB);
		pane.add(exitB);
	}
	
	/**
	 * Event handler for Event Button.
	 */
	private class AddEventButtonHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			AddEventFrame addEventFrame = new AddEventFrame(iCal);
		}
	}
	
	/**
	 * Event handler for To Do Button.
	 */
	private class AddToDoButtonHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			AddToDoFrame addToDoFrame = new AddToDoFrame(iCal);
		}
	}
	
	/**
	 * Event handler for Journal Button.
	 */
	private class AddJournalButtonHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			AddJournalFrame addJournalFrame = new AddJournalFrame(iCal);
		}
	}
	
	/**
	 * Event handler for Free Busy Button.
	 */
	private class AddFreeBusyButtonHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			AddFreeBusyFrame addFreeBusyFrame = new AddFreeBusyFrame(iCal);
		}
	}
	
	/**
	 * Event handler for Time Zone button.
	 */
	private class AddTimeZoneButtonHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			AddTimeZoneFrame addTimeZoneFrame = new AddTimeZoneFrame(iCal);
		}
	}
	
	/**
	 * Event handler for Save button.
	 */
	private class SaveButtonHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			int returnVal = fc.showSaveDialog(ICSFileCreatorStartFrame.this);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
                File file = fc.getSelectedFile();
                iCal.writeICalFile(file);
            }
		}
	}
	
	/**
	 * Event handler for Restart Button.
	 */
	private class RestartButtonHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			iCal = new ICalendar();
		}
	}
	
	/**
	 * Event handler for View Button.
	 */
	private class ViewButtonHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			ViewFrame viewFrame = new ViewFrame(iCal);
		}
	}
	
	/**
	 * Event handler for Exit button.
	 */
	public class ExitButtonHandler implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			System.exit(0);
		}
	}
}